<!DOCTYPE html>
<html lang="">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>账号列表</title>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <script src="../common/header.js"></script>
    <!-- 内容主体区域 -->
    <div id="LAY_app_body">
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">微信号管理&nbsp;&nbsp;总数：<span>1，</span>在线：<span style="color: green;">0，</span>离线：<span style="color: red;">1</span>&nbsp;&nbsp;<a href="add.html" class="layui-btn layui-btn-sm">添加微信</a>&nbsp;&nbsp;
                            <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="batchDeleteWeChat(1)">一键删除被封微信</button>&nbsp;&nbsp;
                            <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="batchDeleteWeChat(2)">一键删除密码错误微信</button>
                            <button class="layui-btn layui-btn layui-btn-sm" onclick="batchWeChatReLogin()">一键微信离线登陆</button>
                        </div>
                        <div class="layui-card-body">
                            <!--检索项-->
                            <form class="layui-form" action="">
                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        <input type="text" name="title" lay-verify="title" placeholder="搜索微信号" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        <select id="group_id1" style="layui-input-inline" onchange="reloadList()">
                                            <option value="0">选择分组</option>
                                            <option value="">系统组</option>
                                            <option value="">超级管理组</option>
                                            <option value="">部门管理组</option>
                                            <option value="">组级管理组</option>
                                            <option value="">普员组</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        <select id="login_status" style="layui-input-inline" onchange="reloadList()">
                                            <option value="9">选择状态</option>
                                            <option value="1">在线</option>
                                            <option value="4">不在服务区</option>
                                            <option value="0">离线</option>
                                            <option value="-1">登录失败</option>
                                            <option value="-2">已登出</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        <select id="changePwd" style="layui-input-inline" onchange="reloadList()">
                                            <option value="9">选择</option>
                                            <option value="0">未改密</option>
                                            <option value="2">已改密</option>
                                        </select>
                                    </div>
                                </div>
                                <button class="layui-btn layui-btn-xs" onclick="reloadList();">搜索</button>
                                <button class="layui-btn layui-btn-xs layui-btn-primary" onclick="reloadList(true);">重置</button>
                            </form>
                            <!--表格内容区域-->
                            <table id="LAY-wechat-manage" lay-filter="LAY-wechat-manage"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        var reloadList;
        var batchDeleteWeChat;
        var batchWeChatReLogin;
        layui.use(['table', 'laytpl', 'form', 'laydate'], function () {
            var $ = layui.$
                , table = layui.table
                , form = layui.form
                , laytpl = layui.laytpl
                , laydate = layui.laydate;

            var groupId = $('#group_id').find("option:selected").val();
            var loginStatus = $('#login_status').find("option:selected").val();
            var search = $('#search').val();
            var changePwd = $('#changePwd').find("option:selected").val();
            var date = $('#LAY-date').val();

            table.render({
                elem: '#LAY-wechat-manage'
//            ,height: 315
                , url: '' //数据接口
                , page: true //开启分页
                , where: { //设定异步数据接口的额外参数，任意设
                    groupId: groupId,
                    loginStatus: loginStatus,
                    changePwd: changePwd,
                    date: date
                }
                , cols: [[ //表头
                    {type: 'checkbox', fixed: 'left'}
                    , {type: 'numbers', title: '序号', fixed: 'left'}
                    , {field: 'name', title: '昵称', width: 220, fixed: 'left', templet: '#wxidTpl'}
                    , {field: 'wxid', title: '微信ID', width: 200, hide: true}
                    , {field: 'groups', title: '分组', width: 200, templet: '#groupTpl'}
                    , {field: 'friend_num', title: '好友数', width: 80}
                    , {field: 'room_num', title: '群数量', width: 80}
                    , {field: 'last_login', title: '最后登录时间', width: 170}
                    , {field: 'msg', title: '提示消息', templet: '#msgTpl'}
                    , {fixed: 'right', width: 270, align: 'center', toolbar: '#barDemo'}
                ]]
                , title: '微信列表'
                , toolbar: '#headBar'
                , defaultToolbar: ['filter', 'exports']
                , data: [{
                    "id": "306597",
                    "status": "1",
                    "last_login": "2019-11-15 13:08:56",
                    "msg": "\u4f60\u5df2\u9000\u51fa\u5fae\u4fe1",
                    "login_status": "-1",
                    "name": "\u5510\u5200",
                    "wxid": "wxid_rcogxu5agnr822",
                    "online_status": "0",
                    "debug": "1",
                    "charge": "0",
                    "expiration_date": null,
                    "is_first_pay": "1",
                    "unread_msg_count": "0",
                    "user_wxid": "rr123456789666666",
                    "request_delete": "0",
                    "upload_friend_limit_get": "0",
                    "smallHeadImage": "http:\/\/wx.qlogo.cn\/mmhead\/ver_1\/rdDydR2LRcrfzJKsDrgZGYuSFBc6lWSDlqK8wWIShH6icLkc01ibRXrKpicFIx0aNQaOlMRicyiaydhqIoK3iaaAticficeh9QLibewMAARvX2utW7ZI\/132",
                    "isExpired": false,
                    "daysLeft": -18216,
                    "login_type": "1",
                    "change_pwd": "0",
                    "friend_num": 8,
                    "friend_num_exclude_wechat": 8,
                    "groups": [{"id": "5297", "name": "好玩", "wechat_num": 1, "wechat_login_num": 0}],
                    "room_num": 0,
                    "rule": {
                        "add_friend_day_limit": "30",
                        "add_friend_interval": "60",
                        "upload_friend_interval": "60",
                        "search_friend_day_limit": "10",
                        "upload_friend_limit_get": "0",
                        "score": "0",
                        "add_chat_room_failed": "0",
                        "add_friend_per_round": "1",
                        "request": "0"
                    },
                    "ways": [{"way_id": "306207", "login_type": "1", "login_status": "0"}],
                    "isManager": 0
                }], "count": 1
            });

            //头工具栏事件
            table.on('toolbar(LAY-wechat-manage)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'deleteSelected':
                        var data = checkStatus.data;
                        var ids = [];
                        layui.each(data, function (index, item) {
                            ids.push(item.id);
                        });
                        layer.confirm('确定要删除选中的微信吗？', function (index) {
                            var post = {
                                del: 1,
                                ids: ids,
                                delete_type: 1
                            };
                            $.ajax({
                                type: "POST",
                                url: "/wechat/deleteSelected",
                                data: post,
                                success: function (response) {
                                    var dataObj = $.parseJSON(response);
                                    if (dataObj.code === 0) {
                                        layer.msg(dataObj.msg, {icon: 6});
                                    } else {
                                        layer.msg(dataObj.msg, {icon: 5});
                                    }

                                    setTimeout(function () {
                                        reloadList(false, true);
                                    }, 1000);
                                },
                                error: function (request, status, error) {

                                }
                            });
                            layer.close(template);
                        });
                        break;
                }
            });

            //监听工具条
            table.on('tool(LAY-wechat-manage)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的DOM对象

                if (layEvent === 'detail') { //查看
                    //do somehing
                } else if (layEvent === 'del') { //删除
                    deleteWeChat(data);
                } else if (layEvent === 'edit') { //编辑
                    //do something
                } else if (layEvent === 'login') {
                    $.ajax({
                        type: "GET",
                        url: "/wechat/newLoginTask/" + data.id,
                        success: function (response) {
                            var dataObj = $.parseJSON(response);
                            if (dataObj.code === 0) {
                                layer.msg(dataObj.msg, {icon: 1});
                            } else {
                                layer.msg(dataObj.msg, {icon: 5});
                            }
                        },
                        error: function (request, status, error) {

                        }
                    });
                } else if (layEvent === 'logout') {
                    layer.confirm('注销后无法对微信执行操作，确认要注销吗？', function (index) {
                        $.ajax({
                            type: "GET",
                            url: "/wechat/notifyLogout/" + data.id,
                            success: function (response) {
                                var dataObj = $.parseJSON(response);
                                if (dataObj.code === 0) {
                                    layer.msg(dataObj.msg, {icon: 1});
                                } else {
                                    layer.msg(dataObj.msg, {icon: 5});
                                }

                                setTimeout(function () {
                                    reloadList(false, true);
                                }, 1000);
                            },
                            error: function (request, status, error) {

                            }
                        });
                        layer.close(template);
                    });
                }
            });

            //监听状态操作
            form.on('switch(statusDemo)', function (obj) {
                var status = 0;
                if (obj.elem.checked) {
                    status = 1;
                }
//            layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
                var id = this.step;
//            console.log(id);
                var post = {
                    id: id,
                    status: status
                };
                $.ajax({
                    type: "POST",
                    url: "/wechat/change_status",
                    data: post,
                    success: function (response) {
                        var dataObj = $.parseJSON(response);
                        if (dataObj.code === 0) {
                            layer.tips('修改成功', obj.othis);
                        } else if (dataObj.code === -1) {
                            layer.tips(dataObj.msg, obj.othis);
                        } else {
                            layer.msg(dataObj.msg, {icon: 5});
                        }
                    },
                    error: function (request, status, error) {

                    }
                });
                return false;
            });

            reloadList = function (reset, stayPage) {
                if (reset == true) {
                    $('#group_id').find('option:first').attr("selected", false);
                    $('#group_id').find('option:first').attr("selected", true);
                    $('#login_status').find('option:first').attr("selected", false);
                    $('#login_status').find('option:first').attr("selected", true);
                    $('#search').val('');
                    $('#changePwd').find('option:first').attr("selected", false);
                    $('#changePwd').find('option:first').attr("selected", true);
                    $('#LAY-date').val('');
                }
                var groupId = $('#group_id').find("option:selected").val();
                var loginStatus = $('#login_status').find("option:selected").val();
                var search = $('#search').val();
                var changePwd = $('#changePwd').find("option:selected").val();
                var date = $('#LAY-date').val();
                if (stayPage == true) {
                    table.reload('LAY-wechat-manage', {
                        where: { //设定异步数据接口的额外参数，任意设
                            groupId: groupId,
                            loginStatus: loginStatus,
                            search: encodeURIComponent(search),
                            changePwd: changePwd,
                            date: date
                        }
                    });
                } else {
                    table.reload('LAY-wechat-manage', {
                        where: { //设定异步数据接口的额外参数，任意设
                            groupId: groupId,
                            loginStatus: loginStatus,
                            search: encodeURIComponent(search),
                            changePwd: changePwd,
                            date: date
                        }
                        , page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                }
            };
            batchDeleteWeChat = function (type) {
                var text = '删除登录失败的微信（最后登录时间在今天之前），确认要执行吗？';
                if (type === 2) {
                    text = '删除密码错误的微信（最后登录时间在今天之前），确认要执行吗？';
                }
                layer.confirm(text, function (index) {
                    var post = {
                        del: 1,
                        type: type
                    };
                    $.ajax({
                        type: "POST",
                        data: post,
                        url: "/wechat/batchDelete",
                        success: function (response) {
                            var dataObj = $.parseJSON(response);
                            if (dataObj.code === 0) {
                                layer.msg(dataObj.msg, {icon: 1});
                            } else {
                                layer.msg(dataObj.msg, {icon: 5});
                            }

                            setTimeout(function () {
                                reloadList(false, true);
                            }, 1000);
                        },
                        error: function (request, status, error) {

                        }
                    });
                    layer.close(template);
                });
            };

            batchWeChatReLogin = function () {
                var text = '确认离线微信重新登陆吗？';
                layer.confirm(text, function (index) {
                    $.ajax({
                        type: "POST",
                        data: {},
                        url: "/wechat/offlineWeChatReLogin",//微信重新登陆
                        success: function (response) {
                            var dataObj = $.parseJSON(response);
                            if (dataObj.code === 0) {
                                layer.msg(dataObj.msg, {icon: 1});
                            } else {
                                layer.msg(dataObj.msg, {icon: 5});
                            }

                            setTimeout(function () {
                                reloadList(false, true);
                            }, 1000);
                        },
                        error: function (request, status, error) {

                        }
                    });
                    layer.close(template);
                });
            };


            laydate.render({
                elem: '#LAY-date'
            });

            var deleteWeChat = function (data) {
                layer.confirm('确定删除该微信吗？', function (index) {
                    var id = data.id;
                    var post = {
                        del: 1,
                        id: id,
                        delete_type: 1
                    };
                    $.ajax({
                        type: "POST",
                        url: "/wechat/delete",
                        data: post,
                        success: function (response) {
                            var dataObj = $.parseJSON(response);
                            if (dataObj.code === 0) {
                                layer.msg(dataObj.msg, {icon: 6});
                            } else {
                                layer.msg(dataObj.msg, {icon: 5});
                            }

                            setTimeout(function () {
                                reloadList(false, true);
                            }, 1000);
                        },
                        error: function (request, status, error) {

                        }
                    });
                    layer.close(template);
                });
                return false;
            };
        });

    </script>
    <script type="text/html" id="wxidTpl">
        <img src="{{d.smallHeadImage || '../assets/images/default_head.jpeg'}}" width="30px" height="30px">
        {{# if(d.login_status == 0){ }}
        <span style="color: red; float: right;">离线</span>
        {{# } else if(d.login_status == 1){ }}
        {{# if(d.online_status == 1){ }}
        <span style="color: green; float: right;">在线</span>
        {{# } else { }}
        <span style="color: orange; float: right;">在线</span>
        {{# } }}
        {{# } }}
        <span style="{{ (d.change_pwd==2 ? 'color:green;' : '') }}">{{d.name}}</span>
    </script>
    <script type="text/html" id="barDemo">
        {{# if(d.isManager == 1){ }}
        <a class="layui-btn layui-btn-xs layui-btn-normal" href="/v1/wechat/viewDeviceInfo/{{d.id}}" target="_blank">设备</a>
        {{# } }}
        {{# if(d.request_delete == 1){ }}
        <span style="color: red;">第二天凌晨删除</span>
        {{# } else if(d.request_delete == 2){ }}
        <span style="color: red;">即将删除</span>
        {{# } else { }}
        <a class="layui-btn layui-btn-xs layui-btn-normal" href="/v1/wechat/info/{{d.id}}" target="_blank">信息</a>
        <a class="layui-btn layui-btn-xs" lay-event="rule" href="/v1/wechat_rule/edit/{{d.id}}" target="_self">规则</a>
        {{# if(d.isManager == 0){ }}
        <a class="layui-btn layui-btn-xs" lay-event="detail" href="/v1/wechat/editGroup/{{d.id}}" target="_self">分组</a>
        {{# if(d.login_type==1 && (d.login_status != 1)){ }}
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="detail" href="/v1/wechat/add_by_qrcode/{{d.id}}"
           target="_self">登录</a>
        {{# } }}
        {{# if((d.login_type==0 || d.login_type==2) && (d.login_status != 1)){ }}
        <a class="layui-btn layui-btn-xs layui-btn-normal" href="/v1/wechat/add_by_pwd/{{d.id}}" target="_self">登录</a>
        {{# } }}
        <button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</button>
        {{# if(d.login_status == 1){ }}
        <button class="layui-btn layui-btn-xs layui-btn-warm" lay-event="logout">注销</button>
        {{# } }}
        {{# } }}
        {{# } }}
    </script>
    <script type="text/html" id="groupTpl">
        {{#  if(d.groups.length>0){ }}
        {{#  var i = 0; }}
        {{#  layui.each(d.groups, function(index, group){ }}
        {{#  if (i!=0) { }}
        ,&nbsp;
        {{#  } }}
        {{#  i++; }}
        <lable>{{group.name}}</lable>
        {{#  }); }}
        {{#  } else { }}
        <lable style="color: orange;">未分组</lable>
        {{# } }}
    </script>
    <script type="text/html" id="msgTpl">
        {{# if (d.login_status == 1 && d.upload_friend_limit_get>0 && d.upload_friend_limit_get>=3) { }}
        {{# } else if (d.login_status==1 && parseInt(d.rule.score)>0 && parseInt(d.rule.add_friend_day_limit)>0 && parseInt(d.rule.score)>=parseInt(d.rule.add_friend_day_limit)) { }}
        <span style="color: blue;">已完成当天加人次数</span>
        {{#  } else { }}
        {{ d.msg }}
        {{# } }}
    </script>
    <script type="text/html" id="headBar">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="deleteSelected">删除选中微信</button>
        </div>
    </script>
</div>
</body>
</html>
